package com.dataex.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.dataex.Constants;

public class DBConnectionRegister {

	public DBConnectionRegister() {

	}

	public static Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName(Constants.DRIVER);
			conn = DriverManager.getConnection(Constants.URL, Constants.USER, Constants.PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static Connection getConnectionByUrl(String url) {
		Connection conn = null;
		try {
			Class.forName(Constants.DRIVER);
			conn = DriverManager.getConnection(url, Constants.USER, Constants.PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * 关闭数据库,释放内存
	 */
	public static void close(Connection conn, java.sql.Statement sta, ResultSet rset) {
		try {
			if (rset != null) {
				rset.close();
			}
			if (sta != null) {
				sta.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
